package com.gen.asynTask;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.com.GameRoomManagerActivity;
import com.gen.tool.AlertDialog_PG;
import com.ouken.com.R;
import com.ouken.tool.ConnectServer;

import android.app.AlertDialog;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.widget.TextView;

public class AsynManagerQuitFR extends AsyncTask<String, TextView, String> {

	private Context context;
	private WifiManager wifi;
	private List params;
	private String httpUrl;
	private TextView roomName;
	private String userEmail;

	private AlertDialog_PG PGDialog;
	private AlertDialog PGnow;
	
	public AsynManagerQuitFR(Context context,TextView roomName,String userEmail) {
		super();
		this.context = context;
		this.roomName = roomName;
		this.userEmail = userEmail;
		this.wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
		this.params = new ArrayList();
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/managerQuitGameRoom";

		PGDialog = new AlertDialog_PG(context,"Please wait \nwhile executing...");
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		System.out.println("AsynManagerQuitFR onpreexcute");

		final AlertDialog PGnow1 = PGDialog.AlertDialog_PG_show();
		PGnow = PGnow1;
        
		params.add(new BasicNameValuePair("roomName", roomName.getText().toString()));
		params.add(new BasicNameValuePair("userEmail", userEmail));
		params.add(new BasicNameValuePair("mac", wifi.getConnectionInfo().getMacAddress()));
		params.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
		
	}
	@Override
	protected String  doInBackground(String...strings ) {
		System.out.println("AsynManagerQuitFR doInBackground");
		return ConnectServer.connect(httpUrl,params);
		
	}

	@Override
    protected void onPostExecute(String result) {
		System.out.println("AsynManagerQuitFR onPostExecute");
		
		final AlertDialog PGnow_c = PGnow;
		PGnow_c.dismiss();
		
		if(result == null){
			 AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("ERROR!")
	                .setMessage(R.string.ERROR_NETWORKOFF)
	                .setPositiveButton("OK",null)
	                .create();
				
			alertDialog.show();
			
			return;
		}
		String[] resultParams =result .split(",");
		
		if(resultParams[0].toString().equals("OK")){
			((GameRoomManagerActivity) context).finish();
			//if have other operates add in here
			return;
			
		}else{
			AlertDialog alertDialog =
				new AlertDialog.Builder(context)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("ERROR!")
                .setMessage(resultParams[1])
                .setPositiveButton("OK",null)
                .create();
			
				alertDialog.show();
			return;
		}
		
    }
	
}
